"""
表单组件
"""
import streamlit as st
from typing import Dict, Any


def input_form(fields: Dict[str, Dict[str, Any]]):
    """
    通用输入表单
    
    Args:
        fields: 字段配置字典
            {
                "field_name": {
                    "type": "text" | "number" | "select" | "date",
                    "label": "显示标签",
                    "options": ["选项1", "选项2"],  # 仅select类型需要
                    "default": 默认值,
                    "required": True/False
                }
            }
    """
    results = {}
    
    for field_name, config in fields.items():
        field_type = config.get("type", "text")
        label = config.get("label", field_name)
        required = config.get("required", False)
        
        if field_type == "text":
            value = st.text_input(
                label + (" *" if required else ""),
                value=config.get("default", "")
            )
        elif field_type == "number":
            value = st.number_input(
                label + (" *" if required else ""),
                value=config.get("default", 0)
            )
        elif field_type == "select":
            value = st.selectbox(
                label + (" *" if required else ""),
                options=config.get("options", []),
                index=config.get("default_index", 0)
            )
        elif field_type == "date":
            value = st.date_input(
                label + (" *" if required else ""),
                value=config.get("default", None)
            )
        else:
            raise ValueError(f"不支持的字段类型: {field_type}")
        
        results[field_name] = value
    
    return results